package kr.co.mcshop.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.mcshop.order.OrderService;
import kr.co.mcshop.product.ProductService;
import kr.co.mcshop.user.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping(value="/admin")
@Controller
public class AdminJumunController {
	
	@Autowired
	OrderService os;
	
	@Autowired
	ProductService ps;
	
	@Autowired
	UserService us;
	
	@RequestMapping(value="/orderList")
	public String jumunList(@RequestParam Map map,Model model){
		List<Map> jumunList = (List<Map>) os.getAllOrder(map);
		HashMap productUserIndex = new HashMap();
		for(int i=0; i<jumunList.size(); i++){
			productUserIndex.put("no", jumunList.get(i).get("product_no"));
			//System.out.println(productUserIndex);
			ArrayList<Map> productListMap = (ArrayList<Map>) ps.getAllProduct(productUserIndex);
			//System.out.println("아이템" + productListMap);
			
			productUserIndex.remove("no");
			System.out.println(jumunList);
			productUserIndex.put("no", jumunList.get(i).get("user_no"));
			//System.out.println(productUserIndex);
			ArrayList<Map> userListMap = (ArrayList<Map>) us.UserList(productUserIndex);
			//System.out.println("회원" + userListMap);
			
			jumunList.get(i).putAll(productListMap.get(0));
			jumunList.get(i).putAll(userListMap.get(0));
		}
		//System.out.println(jumunList);
		model.addAttribute("jumunInfo", jumunList);
		return "admin/order/orderList";
	}
	
	@RequestMapping(value="/orderEdit")
	public String jumunEdit(@RequestParam Map map){
		System.out.println(map);
		if(map.get("state").equals("1")){map.put("state", "주문신청");}
		else if(map.get("state").equals("2")){map.put("state", "주문완료");}
		else if(map.get("state").equals("3")){map.put("state", "주문취소");}
		
		os.UpdateOrder(map);
		
		return "redirect:/admin/orderList";
	}
	
	@RequestMapping(value="/orderDelete")
	public String jumunDelete(@RequestParam Map map){
		System.out.println(map);
		os.deleteOrder(map);
		return "redirect:/admin/orderList";
	}
	
	@RequestMapping(value="/orderDetail")
	public String orderDetail(@RequestParam Map map, Model model){
		List<Map> jumunList = (List<Map>) os.getAllOrder(map);
		HashMap productUserIndex = new HashMap();
		for(int i=0; i<jumunList.size(); i++){
			productUserIndex.put("no", jumunList.get(i).get("product_no"));
			//System.out.println(productUserIndex);
			ArrayList<Map> productListMap = (ArrayList<Map>) ps.getAllProduct(productUserIndex);
			//System.out.println("아이템" + productListMap);
			
			productUserIndex.remove("no");
			
			productUserIndex.put("no", jumunList.get(i).get("user_no"));
			//System.out.println(productUserIndex);
			ArrayList<Map> userListMap = (ArrayList<Map>) us.UserList(productUserIndex);
			//System.out.println("회원" + userListMap);
			
			jumunList.get(i).putAll(productListMap.get(0));
			jumunList.get(i).putAll(userListMap.get(0));
		}
		//System.out.println(jumunList);
		model.addAttribute("jumunInfo", jumunList);
		System.out.println(jumunList);
		return "admin/order/orderInfo";
	}
}
